package com.swgk.core.view.countmenu;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.swgk.core.BR;
import com.swgk.core.R;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.swgk.core.recyclerview.adapter.PageRecycleAdapter;

import java.util.List;


public class CountMenuAdapter extends PageRecycleAdapter<CountMenuEntity> {

    private int resID;//本地资源id

    public CountMenuAdapter(Context context, List<CountMenuEntity> datas) {
        super(context, R.layout.item_count_menu, datas);
    }

    @Override
    public void convert(ItemViewHolder holder, CountMenuEntity entity) {
        holder.setBinding(BR.viewModel, entity);
        ImageView iv = holder.itemView.findViewById(R.id.iv);
        ImageView iv_2 = holder.itemView.findViewById(R.id.iv_2);
        ImageView iv_3 = holder.itemView.findViewById(R.id.iv_3);

       /* String icon = entity.getUrl();
        //判断是否存在
        if (!TextUtils.isEmpty(icon)) {
            //获取到对应的本地图标地址id
            resID = context.getResources().getIdentifier(icon, "drawable", context.getPackageName());
        }
        //判断本地图标地址id是否找到
        if (resID != 0) {
            //Glide加载本地的图标图片iv
            Glide.with(context).load(resID).into(iv);
        } else {
            //Glide加载线上图标
            Glide.with(context).load(entity.getAppIcon()).into(iv);
        }
*/
        if(!TextUtils.isEmpty(entity.getMurl())&&entity.getMurl().contains("http")){
            iv.setVisibility(View.GONE);
            iv_2.setVisibility(View.GONE);
            iv_3.setVisibility(View.VISIBLE);
//            ((TextView) holder.itemView.findViewById(R.id.tv)).setEllipsize(TextUtils.TruncateAt.END);
            Glide.with(context).load(entity.getMurl() + "")
                    .apply(RequestOptions.bitmapTransform(new CircleCrop())
                            .placeholder(R.drawable.icon_default_user)
                            .fallback(R.drawable.null_img_list)
                            .error(R.drawable.null_img_list))
                    .into(iv_3);
        }else
         if (entity.getAppIcon().contains("http")){
            iv_2.setVisibility(View.GONE);
            iv_3.setVisibility(View.GONE);
            iv.setVisibility(View.VISIBLE);
             RequestOptions options = new RequestOptions()
                     .dontAnimate()
                     .placeholder(entity.getImageResource())
                     .error(entity.getImageResource());
             Glide.with(context)
                     .load(entity.getAppIcon())
                     .apply(options)
                     .into(iv);
        }else {
            iv_3.setVisibility(View.GONE);
            iv_2.setVisibility(View.VISIBLE);
            iv.setVisibility(View.GONE);
             RequestOptions options = new RequestOptions()
                     .dontAnimate()
                     .placeholder(entity.getHostImg())
                     .error(entity.getHostImg());
             Glide.with(context)
                     .load(entity.getAppIcon())
                     .apply(options)
                     .into(iv_2);
        }

    }
}
